<?php
/**
 * Created by PhpStorm.
 * User: ZCY
 * Date: 2018/11/26
 * Time: 17:49
 */
namespace api\controllers;
use common\service\RabbitMq;
use PhpAmqpLib\Message\AMQPMessage;
use Yii;
use api\components\ApiController;



class QueueController extends ApiController
{

    public function actionCreateExchange()
    {
        /*    @var RabbitMq $rabbitMq          */
        $rabbitMq = Yii::$app->rabbitMq;
        $rabbitMq->createExchange('direct_test1','direct',false,true);
    }

    public function actionCreateQueue()
    {
        /*    @var RabbitMq $rabbitMq          */
        $rabbitMq = Yii::$app->rabbitMq;
        $rabbitMq->createQueue('queue_test1',false,false,true);
    }

    public function actionQueueBind()
    {
        /*    @var RabbitMq $rabbitMq          */
        $rabbitMq = Yii::$app->rabbitMq;
        $rabbitMq->queueBind('queue_test1','direct_test1','info');
    }

    public function actionSendMessage()
    {
        /*    @var RabbitMq $rabbitMq          */
        $rabbitMq = Yii::$app->rabbitMq;
       /* $rabbitMq->sendMessage('queue_test1','what is this','','direct_test1','info');    //发送到exchage,exchange自动发送到队列
        $rabbitMq->sendMessage('queue_test1','what is this haha');      //直接发送到队列缓冲区(默认的exchange) AMQP default  消息路由到具有 route_key 指定的名称的队列(如果存在)*/

        $properties = array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT);     //消息持久化
        $rabbitMq->sendMessage('queue_test1','what is this haha persistant',$properties);      //直接发送到队列缓冲区

    }

    public function actionReceiveMessage()
    {
        /*    @var RabbitMq $rabbitMq          */
        $rabbitMq = Yii::$app->rabbitMq;
        $data = $rabbitMq->receiveMessage("queue_test1");
        var_dump($data);
        $rabbitMq->deleteMessage($data['delivery_info']['delivery_tag']);
    }




}